package com.elastic.search;

import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.index.query.FilterBuilders.*;

import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
/**
 * filter 只有where 条件的功能
 * @author xiaocai
 *
 */
public class QueryFilter {

	public static FilterBuilder testAddFilter() {
		FilterBuilder filter = andFilter(rangeFilter("age").from(10).to(20),
				prefixFilter("name", "1"));
		return filter;
	}

	/**
	 * boolFilter 值能接受filter
	 * @return
	 */
	public static FilterBuilder testBoolFilter() {
		FilterBuilder filter = boolFilter()
				 .must(termFilter("tag", "wow"))
				 .mustNot(rangeFilter("age").from("10").to("20"))
				 .must(termFilter("tag", "222"))
				.should(termFilter("tag", "sometag"))
				.should(termFilter("tag", "sometagtag"));
		return filter;

	}
	
	/**
	 * 返回一个文档的属性是否存在
	 * @return
	 */
	public static FilterBuilder testExitsFilter(){
		FilterBuilder filter = existsFilter("user");   
		return filter;
	}
	
	/**
	 * 是文档创建的id,不是数据库中的id,一般文档的id, 最好和数据库的id 建立的值一样
	 * 这样好方便搜索。
	 * @return
	 */
	public static FilterBuilder testIdsFilter(){
		FilterBuilder filter = idsFilter("my_type1", "type2")
			    .addIds("1", "4", "100");
		//基于文档过滤器
		filter = typeFilter("my_type"); 
		
		
		
		return filter;
	}
	
	public static void testjingwei(){
		//The location field name
		FilterBuilder filter = geoBoundingBoxFilter("pin.location") 
			    .topLeft(40.73, -74.1)                                  
			    .bottomRight(40.717, -73.99);                           
	}
	
	
	public static void testHaschild(){
		// Has Child termFilter 是否有自孩子
		QueryBuilder qb = (QueryBuilder) hasChildFilter(
		    "blog_tag",                     
		    termFilter("tag","something")   
		);
		
		//
		// Has Parent
		QueryBuilder qb1 = (QueryBuilder)hasParentFilter(
		    "blog",                         
		    termFilter("tag","something")   
		);
	}
	
	public static void  test3(){
		FilterBuilder filter = matchAllFilter();
		
		//不存在user 属性
		FilterBuilder filter2 = missingFilter("user")    
			    .existence(true)                            
			    .nullValue(true);  
		
		FilterBuilder filter3 = notFilter(
			    rangeFilter("price").from("1").to("2")  
			);
		
		FilterBuilder filter4 = orFilter(
		        termFilter("name.second", "banon"), 
		        termFilter("name.nick", "kimchy")   
		    );
		
		//user 的前缀是ki
		FilterBuilder filter5 = prefixFilter(
			    "user", 
			    "ki"    
			);
		
		//FilterBuilders.queryFilter(QueryBuilders.queryString("aa").defaultField("bb"))
		
		FilterBuilder filter6 = queryFilter(
		        queryString("this AND that OR thus")    
		    );
		
		//include lower value means that from is gt when false or gte when true 
		//true 代表from >=
		//<= include upper value means that to is lt when false or lte when true
		// 默认都是true 包含临街点
		FilterBuilder filter7 = rangeFilter("age")   
			    .from("10")                             
			    .to("20")                               
			    .includeLower(true)                     
			    .includeUpper(false);  
		
		// A simplified form using gte, gt, lt or lte
		FilterBuilder filter8 = rangeFilter("age")   
		    .gte("10") 
		    .lt("20");  
		
	}
	
	/**
	 * 判断等于
	 */
	public static void term(){
		FilterBuilder filter9 = termFilter(
			    "user",     
			    "kimchy"    
			);
		
		
		//user in kimchy 或者是elasticsercha
		FilterBuilder filter = termsFilter(
		        "user",             
		        "kimchy",           
		        "elasticsearch"     
		    )
		    .execution("plain");  
		
		//path to nested document
		FilterBuilder filter3 = nestedFilter("obj1",                     
			         boolFilter()                                                
			        .must(termFilter("obj1.name", "blue"))
			        .must(rangeFilter("obj1.count").gt(5))
			    );
	}
	
}
	
